package context.control;

import context.dao.*;
import context.model.Dynamic;
import context.model.Food;
import context.model.FoodClass;
import context.model.FoodSchool;
import context.model.User;
import context.util.ForwardUrl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/")
public class UserControl {

    @Autowired
    private FollowRepository followRepository;

    @Autowired
    private ForwardUrl forwardUrl;

    private UserRepository repository;

    @Autowired
    private DynamicRepository dynamicRepository;

    @Autowired
    private Thumbs_upRepository thumbs_upRepository;

    @Autowired
    private UserinformationRepository userinformationRepository;

    @Autowired
    private FoodclassRepository foodclassRepository;

    @Autowired
    private FoodschoolRepository foodschoolRepository;

    @Autowired
    private RankingListRepository rankingListRepository;

    @Autowired
    HttpSession session;

    @Autowired
    public void setRepository(UserRepository repository) {
        this.repository = repository;
    }

    private boolean isLogined(HttpSession session) {
        Boolean isLogined = (Boolean) session.getAttribute("islogined");
        if (isLogined == null || !isLogined) {
            return false;
        } else {
            return true;
        }
    }

    @RequestMapping(value = "/")
    public String home(@RequestParam(required = false) Long requestDynamicID, Map model) {
//        List<Dynamic> dynamicList = getRandomDynamic();
//
//        if (requestDynamicID != null) {
//            boolean contain = false;
//            for (Dynamic dynamic : dynamicList) {
//                if (dynamic.getDynamic_id() == requestDynamicID) {
//                    contain = true;
//                    break;
//                }
//            }
//            if (!contain) {
//                dynamicList.add(dynamicRepository.findDynamicByID(requestDynamicID));   //添加进去
//            }
//        }
        List<Dynamic> dynamicList = dynamicRepository.getFirstTenDynamic();
        model.put("dynamicList", dynamicList);

        //foodclassRepository.insertFoodInformation(111,"delicious","tmp/png",new Timestamp(System.currentTimeMillis()));
        //foodclassRepository.updateClass(222,"just soso","tmp/png",new Timestamp(System.currentTimeMillis()),4);
        //FoodClass fc = foodclassRepository.findClassByClassid(5);
        //System.out.println("课程信息" + fc);

        //rankingListRepository.insertFood("宽窄巷子",20000l);

        //System.out.println(rankingListRepository.findFoodByFoodname());

        //System.out.println(foodclassRepository.findClassByClassname("水煮鱼"));
        //System.out.println(foodschoolRepository.findSchoolBySchoolname("鸡排教程"));
        return "home";
    }

//    public List<Dynamic> getRandomDynamic() {
//        List<Dynamic> dynamics = dynamicRepository.getDynamicListBeforeTime();
//        return dynamics;
//    }

    @RequestMapping(value = "/home")
    public String getHome(@RequestParam(required = false) Long requestDynamicID, Map model) {
        List<Dynamic> dynamicList = dynamicRepository.getFirstTenDynamic();
        model.put("dynamicList", dynamicList);
        return "home";
    }

    @RequestMapping(value = "/user/{id}")
    public String showUser(@PathVariable("id") long id, Map model){
        System.out.print("路径变量id:"+id);
        User user=repository.findUserByID(id);
        model.put("user",user);
        return "show";
    }

    @RequestMapping(value = "/regist",method = RequestMethod.GET)
    public String regist(){
        return "regist";
    }

    @RequestMapping(value = "/regist",method = RequestMethod.POST,produces="text/html;charset=UTF-8")
    public String delRegist(User user){
        //System.out.print("性别"+user.getGender());
        System.out.println("用户名"+user.getUsername());
        System.out.println("密码"+user.getPassword());

        long id = repository.insertUser(user.getPassword(), user.getUsername());

        session.setAttribute("islogined", true);
        session.setAttribute("username", user.getUsername());

        int fansCount = followRepository.FindFansCount(id);
        int attentionCount = followRepository.FindAtttentionUserCount(id);
        session.setAttribute("attentionCount", attentionCount);
        session.setAttribute("fanCount", fansCount);
        session.setAttribute("userid", id);
        //插入用户的账号密码到数据库中保存
        return  "redirect:/user/"+id;
    }

    @RequestMapping(value = "/login",method = RequestMethod.GET)
    public String loginin(){
        return "login";
    }

    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public String delLoginIn(@RequestParam("username") String username, @RequestParam("password") String password, HttpSession session){
        User user=repository.findUserByUsername(username);

        if(user==null){
            return "login";
        }
        session.setAttribute("islogined",true);
        session.setAttribute("username",username);
        long id=user.getId();
        session.setAttribute("userid", id);
        int fansCount = followRepository.FindFansCount(id);
        int attentionCount = followRepository.FindAtttentionUserCount(id);
        session.setAttribute("attentionCount", attentionCount);
        session.setAttribute("fanCount", fansCount);

        String url=forwardUrl.getForwardUrl();
        if(url.isEmpty())
            return  "redirect:/";
        else{
            return "redirect:/"+url;
        }
    }

    @RequestMapping(value = "/loginout")
    public String delLoginOut(HttpSession session){
        session.setAttribute("islogined",false);    //从会话中删除登录的用户信息
        session.removeAttribute("username");

        return "redirect:/login"; //用户退出后跳转到登录页
    }


    @RequestMapping(value = "/location")
    public String delLocation(){
        return "location";
    }

    @RequestMapping(value="/userpage")
    public String deluserpage(@RequestParam(required = false) String username){

        System.out.println("111"+username);
        if(username!=null&&!username.trim().isEmpty()){
            System.out.println("222"+username);
            session.setAttribute("viewd_user_name",username);
        }else{
            session.setAttribute("viewd_user_name",session.getAttribute("username"));
        }
        System.out.println("333"+session.getAttribute("viewd_user_name"));
        session.setAttribute("currentUser",session.getAttribute("username"));

        return "userpage";
    }

    @RequestMapping(value="/userfans")
    public String delUserFans(Map model,HttpSession session){
        //处理完之后返回userfans
        if(!isLogined(session)){
            forwardUrl.setForwardUrl("userfans");
            return "login";
        }
        return "userfans";
    }

    @RequestMapping(value="/userattentions")
    public String delUserAttentions(Map model,HttpSession session){
        //处理完之后返回userfans
        if(!isLogined(session)){
            forwardUrl.setForwardUrl("userattentions");
            return "login";
        }
        List<User> attentions=new ArrayList<>();
        attentions.add(new User("张勇","帅哥一个"));
        attentions.add(new User("张广伟","小逗比加坑逼一个"));
        attentions.add(new User("土老比一个","他很懒，没有留下任何东西"));
        model.put("attentions",attentions);
        return "userattentions";
    }

    @RequestMapping(value = "/myclassstate")
    public String delUserClassState(HttpSession session, Map model) {
        if(!isLogined(session)){
            forwardUrl.setForwardUrl("myclassstate");
            return "login";
        }

        long userid = (Long) session.getAttribute("userid");
        List<FoodSchool> createFoodSchoolList = foodschoolRepository.findClassByUserid(userid);

        List<FoodSchool> joinedFoodSchoolList = foodschoolRepository.findClassByJoinedUserid(userid);

        model.put("createFoodSchoolList", createFoodSchoolList);
        model.put("joinedFoodSchoolList", joinedFoodSchoolList);

        return "myclassstate";
    }

    public String getAttentionState() {
        return "userattention";
    }
}
